﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using FlyingClub.Models;

namespace FlyingClub.Controllers
{

    // Permet d'effectuer les paiements et de créditer le comptes d'un membre
    public class TransactionsController : BeforeController
    {
        private FlyingClubDBEntities db = new FlyingClubDBEntities();

        //
        // GET: /Transactions/

        /// <summary>
        /// Redirige vers la page d'accueil
        /// </summary>
        /// <returns>this.Redirect("/")</returns>
        public ActionResult Index()
        {
            return this.Redirect("/");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Pay(Member debtor, Member creditor, double payload)
        {
            return View();
        }

        /// <summary>
        /// Donne la vue pour effectuer un paiement
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Pay()
        {
            return View();
        }

        /// <summary>
        /// Crédite le compte de la personne connectée
        /// </summary>
        /// <param name="payload"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Credit(Transaction t)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");

            else if (_access == 1 || _access == 2)
            {
                if (ModelState.IsValid)
                {
                    Session["Payload"] = t.Payload;
                    return Redirect("../expresscheckout.aspx");
                }
            }

            return RedirectToAction("LogOn", "Members");
        }

        /// <summary>
        /// Donne la vue pour créditer le compte de la personne connecté
        /// </summary>
        /// <returns></returns>
        public ActionResult Credit()
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");

            else if (_access == 1 || _access == 2)
            {
                return View();
            }

            return RedirectToAction("LogOn", "Members");
        }

        public ActionResult ValidateCredit()
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");

            else if (_access == 1 || _access == 2)
            {
                if (ModelState.IsValid)
                {
                    int id = (int)Session["ID"];
                    Member member = (from c in db.Member where c.ID == id select c).First();
                    member.Wallet += (int)Session["Payload"];
                    db.SaveChanges();

                    ViewBag.Wallet = member.Wallet;
                    ViewBag.Payload = Session["Payload"];
                    Session["Wallet"] = member.Wallet;
                    Session["Payload"] = null;
                    return View();
                }
            }

            return View();
        }
    }
}
